package Solution.problem061;

import common.ListNode;

/**
 * @program Leetcode
 * @description: 链表环旋转
 * @author: lishangsheng
 * @create: 2019/06/04 09:40
 */
public class Solution {
        private static  ListNode rotateRight(ListNode head, int k) {
            if (k == 0 || head == null) {
                return head;
            }
            ListNode current = head;
            int n = 1;
            while (current.next != null) {
                current = current.next;
                n++;
            }
            //将链表变成一个环
            current.next=head;
            //链表长度减去k,从head开始数n-k
            int step = (n - k) % n;
            if (step < 0) {
                step = step + n;
            }
            //断开环
            while (step > 0) {
                current=current.next;
                head=head.next;
                step--;
            }
            current.next=null;

            return head;
        }
    public static void main(String[] args){
        //1->2->3->4->5
        ListNode listNode1=new ListNode(0);
        ListNode listNode2=new ListNode(1);
        ListNode listNode3=new ListNode(2);
/*        ListNode listNode4=new ListNode(4);
        ListNode listNode5=new ListNode(5);*/
        listNode1.next=listNode2;
        listNode2.next=listNode3;
/*        listNode3.next=listNode4;
        listNode4.next=listNode5;*/

        ListNode result=rotateRight(listNode1,2);
        System.out.println(result.val);
    }
}
